package com.kk.single;

/**
 * 高效的懒汉式
 * double check
 *
 * @author 王志强
 * @date 2021-12-09
 */
public class Singleton3 {

    private static Singleton3 instance;

    private Singleton3() {
    }

    public static Singleton3 getInstance() {
        if (instance == null) {
            synchronized (Singleton3.class) {
                if (instance == null) {
                    instance = new Singleton3();
                }
            }
        }
        return instance;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                Singleton3 instance = Singleton3.getInstance();
                System.out.println("instance =  " + instance + "\n" + instance.hashCode());
            }).start();
        }
    }
}
